package com.enjoyor.soa.traffic.server.tsms.dubbo.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.enjoyor.soa.traffic.api.dto.tsms.RouteInfoDto;
import com.enjoyor.soa.traffic.api.dto.tsms.RoutePointDto;
import com.enjoyor.soa.traffic.api.service.dubbo.tsms.IDubboRouteInfoService;
import com.enjoyor.soa.traffic.core.tsms.domain.CommonRoute;
import com.enjoyor.soa.traffic.core.tsms.domain.RouteInfo;
import com.enjoyor.soa.traffic.core.tsms.domain.RoutePoint;
import com.enjoyor.soa.traffic.core.tsms.service.IRouteInfoService;
import com.enjoyor.soa.traffic.frame.support.util.DomainUtil;
import com.enjoyor.soa.traffic.util.enums.EnumAppCode;
import com.enjoyor.soa.traffic.util.helper.ReflectHelper;
import com.enjoyor.soa.traffic.util.pojo.Page;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;

@SuppressWarnings("rawtypes")
@Service(version = "1.0.0", timeout = 4000)
public class DubboRouteInfoService implements IDubboRouteInfoService {

	@Autowired
	private IRouteInfoService IRouteInfoService;

	@Override
	public ResultPojo getRouteInfoPage(Page page, String routeId, String routeName, String taskId,
			String routeStartPlace, String routeTargetPlace, String routeTypeId) {
		// TODO Auto-generated method stub
		ResultPojo resultPojo = new ResultPojo(IRouteInfoService.getRouteInfoPage(page, routeId, routeName, taskId,
				routeStartPlace, routeTargetPlace, routeTypeId));
		return resultPojo;
	}

	@Override
	public ResultPojo getRouteInfoList(String routeId, String routeName, String taskId, String routeStartPlace,
			String routeTargetPlace, String routeTypeId) {
		// TODO Auto-generated method stub
		List<RouteInfo> list = IRouteInfoService.getRouteInfoList(routeId, routeName, taskId, routeStartPlace,
				routeTargetPlace, routeTypeId);
		return new ResultPojo(ReflectHelper.listToList(list, RouteInfoDto.class));
	}

	@Override
	public ResultPojo insertRouteInfo(RouteInfoDto routeInfoDto, String personName) {
		// TODO Auto-generated method stub
		ResultPojo resultPojo = new ResultPojo();
		List<RoutePointDto> listPointDto = routeInfoDto.getRoutePointList();
		// 线路Dto中的List<RoutePointDto>转化成List<RoutePoint>
		List<RoutePoint> listPoint = DomainUtil.merge2ListJson(listPointDto, RoutePoint.class);
		RouteInfo routeInfo = ReflectHelper.beanToBean(routeInfoDto, RouteInfo.class);
		routeInfo.setRoutePointList(listPoint);
		String routeId = IRouteInfoService.addRouteInfo(routeInfo, personName);
		if( !"false".equals(routeId) ) {
			resultPojo = new ResultPojo(routeId);
		}else {
			resultPojo = new ResultPojo(EnumAppCode.PARAM_ERR, "执行线路已存在或者线路类型参数错误！");
		}
		return resultPojo;
	}

	@Override
	public ResultPojo editRouteInfo(RouteInfoDto routeInfoDto, String personName) {
		// TODO Auto-generated method stub
		ResultPojo resultPojo = new ResultPojo();
		List<RoutePointDto> listPointDto = routeInfoDto.getRoutePointList();
		// 线路Dto中的List<RoutePointDto>转化成List<RoutePoint>
		List<RoutePoint> listPoint = DomainUtil.merge2ListJson(listPointDto, RoutePoint.class);
		RouteInfo routeInfo = ReflectHelper.beanToBean(routeInfoDto, RouteInfo.class);
		routeInfo.setRoutePointList(listPoint);
		Boolean flag = IRouteInfoService.editRouteInfo(routeInfo, personName);
		if(flag) {
			resultPojo = new ResultPojo("线路修改成功！");
		}else {
			resultPojo = new ResultPojo(EnumAppCode.PARAM_ERR, "类型参数错误！");
		}
		return resultPojo;
	}

	@Override
	public ResultPojo deleteRouteInfo(String routeId, String personName) {
		// TODO Auto-generated method stub
		IRouteInfoService.deleteRouteInfo(routeId, personName);
		return new ResultPojo();
	}
    
	@Override
	public ResultPojo changeRouteType(String taskId, String routeId, String personName) {
		// TODO Auto-generated method stub
		IRouteInfoService.changeRouteType(taskId, routeId, personName);
		return new ResultPojo();
	}

	@Override
	public ResultPojo getRouteProcress(String ececuteDate, String taskName, String beforeLicensePlate, String schemeId) {
		// TODO Auto-generated method stub
		List<RouteInfo> list = IRouteInfoService.getRouteProcress(ececuteDate, taskName, beforeLicensePlate, schemeId);
		return new ResultPojo(ReflectHelper.listToList(list, RouteInfoDto.class));
	}

}
